﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data.SqlClient;
using MaxLeaf.DB;
using System.Data;


namespace MaxLeaf.DAL.K3
{
    /// <summary>
    /// 物料
    /// </summary>
    public class ImInvMasterDAL : BaseDAL
    {
        /*
         *Select * From (Select t1.* From t_Item t1  WHERE t1.FDeleteD=0  And t1.FItemClassID = 4 And t1.FItemClassID = 4) i  Where 1=1  And (FNumber = '9.02')
         * select i.* from t_Item i where i.FItemID = 9065
         * select * from t_ICItem where FItemID = 9065
         * 
         * Select FNumber from t_item where FItemClassID = 2001
         * exec sp_executesql N'select FItemID,FName, FLevel, FDetail,FParentID from t_Item where FItemClassID = 4 and FNumber = @P1',N'@P1 varchar(80)','9.02'
         * exec sp_executesql N'SELECT FItemID FROM t_Item WHERE FItemClassID=@P1 AND FNumber=@P2',N'@P1 int,@P2 varchar(80)',4,'9.02.02'
         * 
         * exec sp_executesql N'INSERT INTO t_Item (FItemClassID,FParentID,FLevel,FName,FNumber,FShortNumber,FFullNumber,FDetail,UUID,FDeleted) 
         * VALUES (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8,@P9,@P10)',N'@P1 int,@P2 int,@P3 smallint,@P4 varchar(80),@P5 varchar(80),@P6 varchar(80),@P7 varchar(80),@P8 bit,@P9 varchar(38),@P10 smallint'
         * ,4,9065,3,'test','9.02.02','02','9.02.02',0,'BC43E501-C6C6-4A51-B5D0-462AE2B89E14',0
         * 
         * UPDATE t1  SET t1.FFullName = t2.FFullName FROM t_ICItemBase t1 INNER JOIN t_Item t2 ON t1.FItemID = t2.FItemID AND t2.FItemID =9078
         * 
         * exec sp_executesql N'INSERT INTO t_Item (FItemClassID,FParentID,FLevel,FName,FNumber,FShortNumber,FFullNumber,FDetail,UUID,FDeleted) VALUES (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8,@P9,@P10)',N'@P1 int,@P2 int,@P3 smallint,@P4 varchar(80),@P5 varchar(80),@P6 varchar(80),@P7 varchar(80),@P8 bit,@P9 varchar(38),@P10 smallint'
         * ,2001,9066,3,'test','9.02.02','02','9.02.02',0,'3C453C1F-530F-45EA-8838-84B2A76AEB36',0
         * 
         * Delete from Access_cbCostObj where FItemID=9079
 Insert into Access_cbCostObj(FItemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
 Values(9079,9066,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))
         * 
       
         * 
         * Delete from Access_t_ICItem where FItemID=9078
 Insert into Access_t_ICItem(FItemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
 Values(9078,9065,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))
         * 
         * 
         * UPDATE t1  SET t1.FFullName = t2.FFullName FROM t_ICItemBase t1 INNER JOIN t_Item t2 ON t1.FItemID = t2.FItemID AND t2.FItemID =9078
         * 
         * update t_Item set FName=FName where FItemID=9079 and FItemClassID=2001
         * update t_Item set FName=FName where FItemID=9078 and FItemClassID=4
         */


        /*物料
         * exec sp_executesql N'INSERT INTO t_Item (FItemClassID,FParentID,FLevel,FName,FNumber,FShortNumber,FFullNumber,FDetail,UUID,FDeleted) VALUES (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8,@P9,@P10)',N'@P1 int,@P2 int,@P3 smallint,@P4 varchar(80),@P5 varchar(80),@P6 varchar(80),@P7 varchar(80),@P8 bit,@P9 varchar(38),@P10 smallint'
         * ,4,9078,4,'testwuliao','9.02.02.0000001','0000001','9.02.02.0000001',1,'F2D3CA50-41B1-4D7F-B4C6-CAFD141BE482',0
         
         * exec sp_executesql N'INSERT INTO t_ICItem (FHelpCode,FModel,FAuxClassID,FErpClsID,FTypeID,FUnitGroupID,FUnitID,FOrderUnitID,FSaleUnitID,FProductUnitID,FStoreUnitID,FSecUnitID,FSecCoefficient,FDefaultLoc,FSPID,FSource,FQtyDecimal,FLowLimit,FHighLimit,FSecInv,FUseState,FIsEquipment,FEquipmentNum,FIsSparePart,FFullName,FApproveNo,FAlias,FOrderRector,FPOHighPrice,FPOHghPrcMnyType,FWWHghPrc,FWWHghPrcMnyType,FSOLowPrc,FSOLowPrcMnyType,FIsSale,FProfitRate,FOrderPrice,FSalePrice,FIsSpecialTax,FISKFPeriod,FKFPeriod,FStockTime,FBatchManager,FBookPlan,FBeforeExpire,FCheckCycUnit,FOIHighLimit,FOILowLimit,FSOHighLimit,FSOLowLimit,FInHighLimit,FInLowLimit,FTrack,FPlanPrice,FPriceDecimal,FAcctID,FSaleAcctID,FCostAcctID,FAPAcctID,FAdminAcctID,FGoodSpec,FTaxRate,FCostProject,FIsSNManage,FNote,F_102,F_103,F_104,F_105,F_106,F_107,F_108,F_109,F_110,F_111,F_112,F_113,F_114,F_115,F_116,F_117,F_118,F_119,F_120,F_121,F_122,F_123,F_124,F_125,F_126,F_127,F_128,F_129,F_130,FPlanTrategy,FPlanMode,FOrderTrategy,FFixLeadTime,FLeadTime,FTotalTQQ,FOrderInterVal,FQtyMin,FQtyMax,FBatchAppendQty,FOrderPoint,FBatFixEconomy,FBatChangeEconomy,FRequirePoint,FPlanPoint,FDefaultRoutingID,FDefaultWorkTypeID,FProductPrincipal,FPlanner,FPutInteger,FDailyConsume,FMRPCon,FMRPOrder,FChartNumber,FIsKeyItem,FGrossWeight,FNetWeight,FMaund,FLength,FWidth,FHeight,FSize,FCubicMeasure,FStandardCost,FStandardManHour,FStdPayRate,FChgFeeRate,FStdFixFeeRate,FOutMachFee,FPieceRate,FInspectionLevel,FProChkMde,FWWChkMde,FSOChkMde,FWthDrwChkMde,FStkChkMde,FOtherChkMde,FStkChkPrd,FStkChkAlrm,FInspectionProject,FIdentifier,FVersion,FNameEn,FModelEn,FHSNumber,FFirstUnit,FSecondUnit,FImpostTaxRate,FConsumeTaxRate,FFirstUnitRate,FSecondUnitRate,FIsManage,FManageType,FLenDecimal,FCubageDecimal,FWeightDecimal,FIsCharSourceItem,FCtrlType,FCtrlStraregy,FContainerName,FKanBanCapability,FShortNumber,FNumber,FName,FParentID,FItemID)
         * VALUES (@P1,@P2,@P3,@P4,@P5,@P6,@P7,@P8,@P9,@P10,@P11,@P12,@P13,@P14,@P15,@P16,@P17,@P18,@P19,@P20,@P21,@P22,@P23,@P24,@P25,@P26,@P27,@P28,@P29,@P30,@P31,@P32,@P33,@P34,@P35,@P36,@P37,@P38,@P39,@P40,@P41,@P42,@P43,@P44,@P45,@P46,@P47,@P48,@P49,@P50,@P51,@P52,@P53,@P54,@P55,@P56,@P57,@P58,@P59,@P60,@P61,@P62,@P63,@P64,@P65,@P66,@P67,@P68,@P69,@P70,@P71,@P72,@P73,@P74,@P75,@P76,@P77,@P78,@P79,@P80,@P81,@P82,@P83,@P84,@P85,@P86,@P87,@P88,@P89,@P90,@P91,@P92,@P93,@P94,@P95,@P96,@P97,@P98,@P99,@P100,@P101,@P102,@P103,@P104,@P105,@P106,@P107,@P108,@P109,@P110,@P111,@P112,@P113,@P114,@P115,@P116,@P117,@P118,@P119,@P120,@P121,@P122,@P123,@P124,@P125,@P126,@P127,@P128,@P129,@P130,@P131,@P132,@P133,@P134,@P135,@P136,@P137,@P138,@P139,@P140,@P141,@P142,@P143,@P144,@P145,@P146,@P147,@P148,@P149,@P150,@P151,@P152,@P153,@P154,@P155,@P156,@P157,@P158,@P159,@P160,@P161,@P162,@P163,@P164,@P165,@P166,@P167,@P168,@P169,@P170)',N'@P1 varchar(20),@P2 varchar(255),@P3 int,@P4 int,@P5 int,@P6 int,@P7 int,@P8 int,@P9 int,@P10 int,@P11 int,@P12 int,@P13 float,@P14 int,@P15 int,@P16 int,@P17 smallint,@P18 float,@P19 float,@P20 float,@P21 int,@P22 bit,@P23 varchar(80),@P24 bit,@P25 varchar(255),@P26 varchar(80),@P27 varchar(80),@P28 int,@P29 float,@P30 int,@P31 float,@P32 int,@P33 float,@P34 int,@P35 bit,@P36 float,@P37 float,@P38 float,@P39 bit,@P40 bit,@P41 int,@P42 bit,@P43 bit,@P44 bit,@P45 int,@P46 int,@P47 float,@P48 float,@P49 float,@P50 float,@P51 float,@P52 float,@P53 int,@P54 float,@P55 smallint,@P56 int,@P57 int,@P58 int,@P59 int,@P60 int,@P61 int,@P62 int,@P63 int,@P64 bit,@P65 varchar(80),@P66 varchar(80),@P67 int,@P68 varchar(254),@P69 varchar(254),@P70 varchar(50),@P71 float,@P72 varchar(999),@P73 varchar(999),@P74 float,@P75 varchar(10),@P76 varchar(10),@P77 varchar(10),@P78 varchar(10),@P79 varchar(10),@P80 varchar(10),@P81 varchar(10),@P82 varchar(10),@P83 varchar(10),@P84 varchar(10),@P85 varchar(10),@P86 varchar(50),@P87 int,@P88 varchar(254),@P89 varchar(254),@P90 varchar(999),@P91 varchar(999),@P92 float,@P93 float,@P94 varchar(999),@P95 int,@P96 int,@P97 int,@P98 real,@P99 real,@P100 int,@P101 int,@P102 float,@P103 float,@P104 float,@P105 float,@P106 float,@P107 float,@P108 int,@P109 int,@P110 int,@P111 int,@P112 int,@P113 int,@P114 bit,@P115 float,@P116 bit,@P117 bit,@P118 varchar(80),@P119 bit,@P120 float,@P121 float,@P122 int,@P123 float,@P124 float,@P125 float,@P126 float,@P127 int,@P128 float,@P129 float,@P130 float,@P131 float,@P132 float,@P133 float,@P134 float,@P135 int,@P136 int,@P137 int,@P138 int,@P139 int,@P140 int,@P141 int,@P142 int,@P143 int,@P144 int,@P145 int,@P146 varchar(10),@P147 varchar(255),@P148 varchar(255),@P149 int,@P150 varchar(255),@P151 varchar(255),@P152 float,@P153 float,@P154 float,@P155 float,@P156 bit,@P157 int,@P158 int,@P159 int,@P160 int,@P161 int,@P162 int,@P163 int,@P164 varchar(50),@P165 int,@P166 varchar(80),@P167 varchar(80),@P168 varchar(80),@P169 int,@P170 int'
         * ,NULL,NULL,0,1,40005,1437,1438,1438,1439,1438,1438,0,0,0,0,0,4,0,1000,0,341,0,NULL,0,NULL,NULL,NULL,0,0,1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,76,0,2,1016,1073,1078,0,0,0,17,0,0,NULL,NULL,1,NULL,NULL,NULL,0,NULL,NULL,0,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,0,NULL,NULL,NULL,NULL,0,0,NULL,321,14036,331,0,1,0,0,1,10000,1,0,0,1,1,1,0,0,0,0,0,0,1,0,NULL,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,352,352,352,352,352,352,352,9999,0,0,0,NULL,NULL,NULL,0,NULL,NULL,0,0,0,0,0,0,2,4,2,0,14039,0,NULL,1,'0000001','9.02.02.0000001','testwuliao',9078,9080
         
         * Delete from Access_t_ICItem where FItemID=9080
         Insert into Access_t_ICItem(FItemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
         Values(9080,9078,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))
         
         * UPDATE t1  SET t1.FFullName = t2.FFullName FROM t_ICItemBase t1 INNER JOIN t_Item t2 ON t1.FItemID = t2.FItemID AND t2.FItemID =9080
         * 
         * update t_Item set FName=FName where FItemID=9080 and FItemClassID=4
         */
        public ImInvMasterDAL()
            : base()
        {
            strTableName = "t_ICItem";
            strFieldNames = @"FDeleted,FHelpCode,FModel,FAuxClassID,FErpClsID,FTypeID,FUnitGroupID,FUnitID,FOrderUnitID,FSaleUnitID,FProductUnitID,FStoreUnitID,FSecUnitID,FSecCoefficient,FDefaultLoc,FSPID,FSource,FQtyDecimal,FLowLimit,FHighLimit,FSecInv,FUseState,FIsEquipment,FEquipmentNum,FIsSparePart,FFullName,FApproveNo,FAlias,FOrderRector,FPOHighPrice,FPOHghPrcMnyType,FWWHghPrc,FWWHghPrcMnyType,FSOLowPrc,FSOLowPrcMnyType,FIsSale,FProfitRate,FOrderPrice,FSalePrice,FIsSpecialTax,FISKFPeriod,FKFPeriod,FStockTime,FBatchManager,FBookPlan,FBeforeExpire,FCheckCycUnit,FOIHighLimit,FOILowLimit,FSOHighLimit,FSOLowLimit,FInHighLimit,FInLowLimit,FTrack,FPlanPrice,FPriceDecimal,FAcctID,FSaleAcctID,FCostAcctID,FAPAcctID,FAdminAcctID,FGoodSpec,FTaxRate,FCostProject,FIsSNManage,FNote,F_102,F_103,F_104,F_105,F_106,F_107,F_108,F_109,F_110,F_111,F_112,F_113,F_114,F_115,F_116,F_117,F_118,F_119,F_120,F_121,F_122,F_124,F_125,F_126,F_127,F_128,F_129,F_130,f_131,f_132,f_133,f_134,f_135,f_136,F_137,f_140,FPlanTrategy,FPlanMode,FOrderTrategy,FFixLeadTime,FLeadTime,FTotalTQQ,FOrderInterVal,FQtyMin,FQtyMax,FBatchAppendQty,FOrderPoint,FBatFixEconomy,FBatChangeEconomy,FRequirePoint,FPlanPoint,FDefaultRoutingID,FDefaultWorkTypeID,FProductPrincipal,FPlanner,FPutInteger,FDailyConsume,FMRPCon,FMRPOrder,FChartNumber,FIsKeyItem,FGrossWeight,FNetWeight,FMaund,FLength,FWidth,FHeight,FSize,FCubicMeasure,FStandardCost,FStandardManHour,FStdPayRate,FChgFeeRate,FStdFixFeeRate,FOutMachFee,FPieceRate,FInspectionLevel,FProChkMde,FWWChkMde,FSOChkMde,FWthDrwChkMde,FStkChkMde,FOtherChkMde,FStkChkPrd,FStkChkAlrm,FInspectionProject,FIdentifier,FVersion,FNameEn,FModelEn,FHSNumber,FFirstUnit,FSecondUnit,FImpostTaxRate,FConsumeTaxRate,FFirstUnitRate,FSecondUnitRate,FIsManage,FManageType,FLenDecimal,FCubageDecimal,FWeightDecimal,FIsCharSourceItem,FCtrlType,FCtrlStraregy,FContainerName,FKanBanCapability,FShortNumber,FNumber,FName,FParentID";

        }
        //因为有多个unit,保存unitid对应，不用每次从数据库取
        Dictionary<string, string> _dictUnit = null;

        public override void Update(DBAccess dbOrgCon, DBAccess dbCon, string strKeyValue)
        {
            _dictUnit = new Dictionary<string, string>();
            base.Update(dbOrgCon, dbCon, strKeyValue);

        }

        protected override void Update_Before(DBAccess dbOrgCon, DBAccess dbCon, DataRow drOrgData, DataRow drData)
        {
            string strSql = string.Empty;
            KeyValuePair<string, IDataParameter[]> kvp = new KeyValuePair<string, IDataParameter[]>();

            strSql = "Update t_ICItem Set FParentID =  @FParentID  Where  FItemID=@FItemID";
            kvp = dbCon.CreateCondParam(new Dictionary<string, object>() { { "FParentID", drData["FParentID"] }, { "FItemID", drData["FItemID"] } });
            dbCon.ExecuteSql(strSql, kvp.Value);

            strSql = "Update t_Item Set FParentID =  @FParentID ,FDeleted=@FDeleted,Fnumber=@Fnumber,FName=@FName,FShortNumber=@FShortNumber,FFullNumber=@Fnumber Where  FItemID=@FItemID";
            kvp = dbCon.CreateCondParam(new Dictionary<string, object>() { { "FParentID", drData["FParentID"] }, { "FFullName", drData["FFullName"] }, { "FDeleted", drData["FDeleted"] }, { "Fnumber", drData["Fnumber"] }, { "FName", drData["FName"] }, { "FShortNumber", drData["FShortNumber"] }, { "FItemID", drData["FItemID"] } });
            dbCon.ExecuteSql(strSql, kvp.Value);
        }
        protected override void UpdateOtherData(DBAccess dbOrgCon, DBAccess dbCon, DataRow drOrgData, DataRow drData)
        {
            string strSql = string.Empty;
            KeyValuePair<string, IDataParameter[]> kvp = new KeyValuePair<string, IDataParameter[]>();
            ////是否禁用
            //strSql = "Update t_Item Set FDeleted=@FDeleted Where not FDeleted=@FDeleted And FItemID=@FItemID";
            //kvp = dbCon.CreateCondParam(new Dictionary<string, object>() { { "FDeleted", drData["FDeleted"] }, { "FItemID", drData["FItemID"] } });
            //dbCon.ExecuteSql(strSql, kvp.Value);



            //; Update t_ICItem Set FDeleted=1 Where FDeleted=0 And FItemID=44739

            ////触发器修改全名
            // strSql = "UPDATE t1  SET t1.FFullName = t2.FFullName FROM t_ICItemBase t1 INNER JOIN t_Item t2 ON t1.FItemID = t2.FItemID AND t2.FItemID =@FItemID";
            // kvp = dbCon.CreateCondParam(new Dictionary<string, object>() { { "FItemID", drData["FItemID"] } });
            //dbCon.ExecuteSql(strSql, kvp.Value);


            //update barcode
            strSql = "select * from t_barcode where FTypeID=4 and FItemID=@FItemID";
            kvp = dbOrgCon.CreateCondParam(new Dictionary<string, object>() { { "FItemID", drOrgData["FItemID"] } });

            t_barcodeDAL barcoe = new t_barcodeDAL();
            DataTable dtBarCode = dbOrgCon.GetDataTable(strSql, kvp.Value);
            if (dtBarCode != null && dtBarCode.Rows.Count > 0)
            {
                foreach (DataRow drBarCode in dtBarCode.Rows)
                {
                    barcoe.Update(dbOrgCon, dbCon, CvtFunc.ToString(drBarCode["fid"]));
                }
            }

        }


        protected override void DAL_GetFieldData(DBAccess dbOrgCon, DBAccess dbDestCon, string strFieldName, DataRow drOrgData, DataRow drDestData)
        {
            if (strFieldName.Equals("FParentID", StringComparison.InvariantCultureIgnoreCase))
            {
                //根据fnumber得到上级组
                //物料上级组有4和2001两种
                string[] arrFnumber = CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
                if (arrFnumber.Length > 1)
                {
                    drDestData[strFieldName] = GetFParentID("4", dbOrgCon, dbDestCon, arrFnumber);

                    GetFParentID("2001", dbOrgCon, dbDestCon, arrFnumber);
                }
                else
                {
                    drDestData[strFieldName] = "0";
                };
            }
            else if (strFieldName.Equals("FItemID", StringComparison.InvariantCultureIgnoreCase))
            {

                string[] arrFnumber = CvtFunc.ToString(drOrgData["Fnumber"]).Split('.');
                string strFParentID = CvtFunc.ToString(drDestData["FParentID"]);
                if (strFParentID.Length == 0)
                {
                    //先得到上级组
                    DAL_GetFieldData(dbOrgCon, dbDestCon, "FParentID", drOrgData, drDestData);
                    strFParentID = CvtFunc.ToString(drDestData["FParentID"]);
                }

                //再fitemid
                drDestData[strFieldName] = GetFItemID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData["FParentID"]), strFParentID, arrFnumber);


            }
            else if (strFieldName.Equals("FUnitGroupID", StringComparison.InvariantCultureIgnoreCase))
            {
                //unit组，每组有一批单位
                string strFUnitGroupID = GetFUnitGroupID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));
                if (strFUnitGroupID.Length == 0)
                {
                    drDestData[strFieldName] = DBNull.Value;
                }
                else
                {
                    drDestData[strFieldName] = strFUnitGroupID;
                    //默认单位
                    SetDefaulunitid(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]), strFUnitGroupID);
                }


            }
            else if (strFieldName.Equals("FTypeID", StringComparison.InvariantCultureIgnoreCase))
            {

                //物料类别
                drDestData[strFieldName] = GetFTypeID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));


            }
            else if (strFieldName.Equals("f_136", StringComparison.InvariantCultureIgnoreCase))
            {

                //物料类别
                drDestData[strFieldName] = GetFTypeID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));


            }
            else if (strFieldName.EndsWith("UnitID", StringComparison.InvariantCultureIgnoreCase))
            {
                //单位
                string strOrgFUnitID = CvtFunc.ToString(drOrgData[strFieldName]);
                string strFUnitID = string.Empty;
                if (_dictUnit != null && _dictUnit.ContainsKey(strOrgFUnitID))
                {
                    strFUnitID = _dictUnit[strOrgFUnitID];
                }
                else
                {
                    strFUnitID = GetFUnitID(dbOrgCon, dbDestCon, strOrgFUnitID, CvtFunc.ToString(drDestData["FUnitGroupID"]));
                }


                if (strFUnitID.Length == 0)
                {
                    drDestData[strFieldName] = DBNull.Value;
                }
                else
                {
                    drDestData[strFieldName] = strFUnitID;
                }

                if (_dictUnit != null && !_dictUnit.ContainsKey(strOrgFUnitID))
                {
                    _dictUnit.Add(strOrgFUnitID, strFUnitID);
                }

            }
            else if (strFieldName.EndsWith("AcctID", StringComparison.InvariantCultureIgnoreCase))
            {


                drDestData[strFieldName] = GetAcctID(dbOrgCon, dbDestCon, CvtFunc.ToString(drOrgData[strFieldName]));


            }
            else
            {
                base.DAL_GetFieldData(dbOrgCon, dbDestCon, strFieldName, drOrgData, drDestData);
            }
        }

        private string GetAcctID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgAcctId)
        {
            if (strOrgAcctId.Length == 0 || strOrgAcctId.Equals("0"))
            {
                return "0";
            }
            t_AccountDAL dal = new t_AccountDAL();
            return dal.GetDestMapKeyValue(dbOrgCon, dbDestCon, strOrgAcctId);
        }

        private void SetDefaulunitid(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFUnitGroupID, string strDestFUnitGroupID)
        {
            t_UnitGroupDAL dalGroup = new t_UnitGroupDAL();
            DataRow drOrgGroup = dalGroup.Gett_UnitGroup(dbOrgCon, strOrgFUnitGroupID);
            if (drOrgGroup == null)
            {
                return;
            }
            string strOrgFUnitID = CvtFunc.ToString(drOrgGroup["FDefaultUnitID"]);
            if (strOrgFUnitID.Equals("0"))
            {
                return;
            }


            string strFUnitID = string.Empty;
            if (_dictUnit != null && _dictUnit.ContainsKey(strOrgFUnitID))
            {
                strFUnitID = _dictUnit[strOrgFUnitID];
            }
            else
            {
                strFUnitID = GetFUnitID(dbOrgCon, dbDestCon, strOrgFUnitID, strDestFUnitGroupID);
            }


            if (strFUnitID.Length == 0)
            {

            }
            else
            {
                dalGroup.SetDefaulunitid(dbDestCon, strDestFUnitGroupID, strFUnitID);
            }

            if (_dictUnit != null && !_dictUnit.ContainsKey(strOrgFUnitID))
            {
                _dictUnit.Add(strOrgFUnitID, strFUnitID);
            }

        }

        private string GetFTypeID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFInterID)
        {
            t_SubMessageDAL dal = new t_SubMessageDAL();
            return dal.GetFInterID(dbOrgCon, dbDestCon, strOrgFInterID);
        }

        private string GetFUnitID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFUnitID, string strFParentID)
        {
            t_MeasureUnitDAL dal = new t_MeasureUnitDAL();
            return dal.GetFMeasureUnitID(dbOrgCon, dbDestCon, strOrgFUnitID, strFParentID);
        }

        private string GetFUnitGroupID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFUnitGroupID)
        {
            t_UnitGroupDAL dal = new t_UnitGroupDAL();
            return dal.GetFUnitGroupID(dbOrgCon, dbDestCon, strOrgFUnitGroupID);

        }

        private string GetFParentID(string strFItemClassID, DBAccess dbOrgCon, DBAccess dbDestCon, string[] arrFnumber)
        {

            const int intFDetail = 0;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL();
            for (int i = 0; i < arrFnumber.Length - 1; i++)
            {
                string strFParentID = "0";
                if (i != 0)
                {
                    strFParentID = strFItemId;
                }
                //得到每级的FItemId
                strFItemId = dal.GetFItemId(dbOrgCon, dbDestCon, strFItemClassID, i + 1, string.Empty, strFParentID, string.Join(".", arrFnumber, 0, i + 1), intFDetail);
                if (strFItemClassID.Equals("4"))
                {
                    UpdateAccess_t_ICItem(dbDestCon, strFItemId, strFParentID);
                }
                else
                {
                    UpdateAccess_cbCostObj(dbDestCon, strFItemId, strFParentID);
                }



            }

            return strFItemId;
        }

        private void UpdateAccess_t_ICItem(DBAccess dbDestCon, string strFItemId, string strFParentID)
        {
            string strSql = @"
                if not exists(select * from Access_t_ICItem where FItemID=@FItemID)
                begin
                 Insert into Access_t_ICItem(FItemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
                 Values(@FItemID,@FParentIDX,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))

                end
                ";
            dbDestCon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FItemID", strFItemId), new SqlParameter("@FParentIDX", strFParentID) });
        }

        private void UpdateAccess_cbCostObj(DBAccess dbDestCon, string strFItemId, string strFParentID)
        {
            string strSql = @"
                if not exists(select * from Access_cbCostObj where FItemID=@FItemID)
                begin
                 Insert into Access_cbCostObj(FItemID,FParentIDX,FDataAccessView,FDataAccessEdit,FDataAccessDelete)
                Values(@FItemID,@FParentIDX,convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)),convert(varbinary(7200),REPLICATE(char(255),100)))

                end
                ";
            dbDestCon.ExecuteSql(strSql, new SqlParameter[] { new SqlParameter("@FItemID", strFItemId), new SqlParameter("@FParentIDX", strFParentID) });
        }
        private string GetFItemID(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFParentID, string strFParentID, string[] arrFnumber)
        {
            const string strFItemClassID = "4";
            const int intFDetail = 1;
            string strFItemId = string.Empty;
            t_ItemDAL dal = new t_ItemDAL();


            strFItemId = dal.GetFItemId(dbOrgCon, dbDestCon, strFItemClassID, arrFnumber.Length, strOrgFParentID, strFParentID, string.Join(".", arrFnumber, 0, arrFnumber.Length), intFDetail);

            UpdateAccess_t_ICItem(dbDestCon, strFItemId, strFParentID);


            return strFItemId;
        }

        public override string GetDestMapKeyValue(DBAccess dbOrgCon, DBAccess dbDestCon, string strOrgFItemID)
        {
            if (dbDestCon.ServerMasterDB)
            {
                return strOrgFItemID;
            }

            KeyValuePair<string, IDataParameter[]> kvp = dbDestCon.CreateCondParam(new Dictionary<string, object>() { { "TableName", "t_item" }, { "OrgKeyValue", strOrgFItemID } });
            string strSql = string.Format("select DestKeyValue from mkeymap where {0}", kvp.Key);

            string strFitemid = dbDestCon.GetValue(strSql, kvp.Value);
            if (strFitemid.Length > 0)
            {
                return strFitemid;
            }

            Update(dbOrgCon, dbDestCon, strOrgFItemID);

            kvp = dbDestCon.CreateCondParam(new Dictionary<string, object>() { { "TableName", "t_item" }, { "OrgKeyValue", strOrgFItemID } });
            return dbDestCon.GetValue(strSql, kvp.Value);
        }
    }
}